// 两数之和
// on^2  不可用
function findTowNumbers1(arr: number[], n: number): number[] {
    const res: number[] = [];
    const length = arr.length;
    if (length === 0) return res;
    for (let i = 0; i < length - 1; i++) {
        const n1 = arr[i];
        let flag = false;
        for (let j = i + 1; j < length; j++) {
            const n2 = arr[j];
            if (n1 + n2 === n) {
                res.push(n1);
                res.push(n2);
                flag = true;
                break
            }
        }
        if (flag) break
    }
    return res
}

// 查找两个数和为n的数(双指针)  on
function findTowNumbers2(arr: number[], n: number): number[] {
    const res: number[] = [];
    const length = arr.length;
    if (length === 0) return res;
    let i = 0; //头
    let j = length - 1; //尾
    while (i < j) {
        const n1 = arr[i]
        const n2 = arr[j]
        const sum = n1 + n2;
        if (sum > n) {
            j--
        } else if (sum < n) {
            i++
        } else {
            res.push(n1);
            res.push(n2);
            break
        }
    }
    return res


}
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 6];
console.log(findTowNumbers2(data, 9));
